1   /*
2    * Copyright (C) 2007 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import static com.google.common.truth.Truth.assertThat;
20  
21  import com.google.common.annotations.GwtCompatible;
22  
23  import junit.framework.TestCase;
24  
25  import java.util.Arrays;
26  import java.util.List;
27  
28  /**
29   * Unit test for {@code ObjectArrays}.
30   *
31   * @author Kevin Bourrillion
32   */
33  @GwtCompatible(emulated = true)
34  public class ObjectArraysTest extends TestCase {
35  
36    public void testNewArray_fromArray_Empty() {
37      String[] in = new String[0];
38      String[] empty = ObjectArrays.newArray(in, 0);
39      assertEquals(0, empty.length);
40    }
41  
42    public void testNewArray_fromArray_Nonempty() {
43      String[] array = ObjectArrays.newArray(new String[0], 2);
44      assertEquals(String[].class, array.getClass());
45      assertEquals(2, array.length);
46      assertNull(array[0]);
47    }
48  
49    public void testNewArray_fromArray_OfArray() {
50      String[][] array = ObjectArrays.newArray(new String[0][0], 1);
51      assertEquals(String[][].class, array.getClass());
52      assertEquals(1, array.length);
53      assertNull(array[0]);
54    }
55  
56    public void testToArrayImpl1() {
57      doTestToArrayImpl1(Lists.<Integer>newArrayList());
58      doTestToArrayImpl1(Lists.newArrayList(1));
59      doTestToArrayImpl1(Lists.newArrayList(1, null, 3));
60    }
61  
62    private void doTestToArrayImpl1(List<Integer> list) {
63      Object[] reference = list.toArray();
64      Object[] target = ObjectArrays.toArrayImpl(list);
65      assertEquals(reference.getClass(), target.getClass());
66      assertTrue(Arrays.equals(reference, target));
67    }
68  
69    public void testToArrayImpl2() {
70      doTestToArrayImpl2(Lists.<Integer>newArrayList(), new Integer[0], false);
71      doTestToArrayImpl2(Lists.<Integer>newArrayList(), new Integer[1], true);
72  
73      doTestToArrayImpl2(Lists.newArrayList(1), new Integer[0], false);
74      doTestToArrayImpl2(Lists.newArrayList(1), new Integer[1], true);
75      doTestToArrayImpl2(Lists.newArrayList(1), new Integer[] { 2, 3 }, true);
76  
77      doTestToArrayImpl2(Lists.newArrayList(1, null, 3), new Integer[0], false);
78      doTestToArrayImpl2(Lists.newArrayList(1, null, 3), new Integer[2], false);
79      doTestToArrayImpl2(Lists.newArrayList(1, null, 3), new Integer[3], true);
80    }
81  
82    private void doTestToArrayImpl2(List<Integer> list, Integer[] array1,
83        boolean expectModify) {
84      Integer[] starting = ObjectArrays.arraysCopyOf(array1, array1.length);
85      Integer[] array2 = ObjectArrays.arraysCopyOf(array1, array1.length);
86      Object[] reference = list.toArray(array1);
87  
88      Object[] target = ObjectArrays.toArrayImpl(list, array2);
89  
90      assertEquals(reference.getClass(), target.getClass());
91      assertTrue(Arrays.equals(reference, target));
92      assertTrue(Arrays.equals(reference, target));
93  
94      Object[] expectedArray1 = expectModify ? reference : starting;
95      Object[] expectedArray2 = expectModify ? target : starting;
96      assertTrue(Arrays.equals(expectedArray1, array1));
97      assertTrue(Arrays.equals(expectedArray2, array2));
98    }
99  
100   public void testPrependZeroElements() {
101     String[] result = ObjectArrays.concat("foo", new String[] {});
102     assertThat(result).asList().has().item("foo");
103   }
104 
105   public void testPrependOneElement() {
106     String[] result = ObjectArrays.concat("foo", new String[] { "bar" });
107     assertThat(result).asList().has().exactly("foo", "bar").inOrder();
108   }
109 
110   public void testPrependTwoElements() {
111     String[] result = ObjectArrays.concat("foo", new String[] { "bar", "baz" });
112     assertThat(result).asList().has().exactly("foo", "bar", "baz").inOrder();
113   }
114 
115   public void testAppendZeroElements() {
116     String[] result = ObjectArrays.concat(new String[] {}, "foo");
117     assertThat(result).asList().has().item("foo");
118   }
119 
120   public void testAppendOneElement() {
121     String[] result = ObjectArrays.concat(new String[] { "foo" }, "bar");
122     assertThat(result).asList().has().exactly("foo", "bar").inOrder();
123   }
124 
125   public void testAppendTwoElements() {
126     String[] result = ObjectArrays.concat(new String[] { "foo", "bar" }, "baz");
127     assertThat(result).asList().has().exactly("foo", "bar", "baz").inOrder();
128   }
129 
130   public void testEmptyArrayToEmpty() {
131     doTestNewArrayEquals(new Object[0], 0);
132   }
133 
134   public void testEmptyArrayToNonEmpty() {
135     checkArrayEquals(new Long[5], ObjectArrays.newArray(new Long[0], 5));
136   }
137 
138   public void testNonEmptyToShorter() {
139     checkArrayEquals(new String[9], ObjectArrays.newArray(new String[10], 9));
140   }
141 
142   public void testNonEmptyToSameLength() {
143     doTestNewArrayEquals(new String[10], 10);
144   }
145 
146   public void testNonEmptyToLonger() {
147     checkArrayEquals(new String[10],
148         ObjectArrays.newArray(new String[] { "a", "b", "c", "d", "e" }, 10));
149   }
150 
151   private static void checkArrayEquals(Object[] expected, Object[] actual) {
152     assertTrue("expected(" + expected.getClass() + "): " + Arrays.toString(expected)
153         + " actual(" + actual.getClass() + "): " + Arrays.toString(actual),
154         arrayEquals(expected, actual));
155   }
156 
157   private static boolean arrayEquals(Object[] array1, Object[] array2) {
158     assertSame(array1.getClass(), array2.getClass());
159     return Arrays.equals(array1, array2);
160   }
161 
162   private static void doTestNewArrayEquals(Object[] expected, int length) {
163     checkArrayEquals(expected, ObjectArrays.newArray(expected, length));
164   }
165 }
166